var $parentNode = window.parent.document;

function $childNode(name) {
    return window.frames[name]
}

// tooltips
$('.tooltip-demo').tooltip({
    selector: "[data-toggle=tooltip]",
    container: "body"
});

// 使用animation.css修改Bootstrap Modal
$('.modal').appendTo("body");

$("[data-toggle=popover]").popover();

//折叠ibox
$('.collapse-link').click(function () {
    var ibox = $(this).closest('div.ibox');
    var button = $(this).find('i');
    var content = ibox.find('div.ibox-content');
    content.slideToggle(200);
    button.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down');
    ibox.toggleClass('').toggleClass('border-bottom');
    setTimeout(function () {
        ibox.resize();
        ibox.find('[id^=map-]').resize();
    }, 50);
});

//关闭ibox
$('.close-link').click(function () {
    var content = $(this).closest('div.ibox');
    content.remove();
});

//判断当前页面是否在iframe中
if (top == this) {
    var gohome = '<div class="gohome"><a class="animated bounceInUp" href="index.html?v=4.0" title="返回首页"><i class="fa fa-home"></i></a></div>';
    $('body').append(gohome);
}

//animation.css
function animationHover(element, animation) {
    element = $(element);
    element.hover(
        function () {
            element.addClass('animated ' + animation);
        },
        function () {
            //动画完成之前移除class
            window.setTimeout(function () {
                element.removeClass('animated ' + animation);
            }, 2000);
        });
}

//拖动面板
function WinMove() {
    var element = "[class*=col]";
    var handle = ".ibox-title";
    var connect = "[class*=col]";
    $(element).sortable({
            handle: handle,
            connectWith: connect,
            tolerance: 'pointer',
            forcePlaceholderSize: true,
            opacity: 0.8,
        })
        .disableSelection();
}

/**
 * 字符串转换成JSON
 * @param env
 */
function stringToJson(env) {
    return JSON.parse(env);
}

/**
 * 检查是否包含指定字符串
 * @param env
 * @param string // 要查找字符串
 * @returns {boolean}
 */
function checkString(env, string) {
    var regExp = new RegExp(string);
    return regExp.test(env);
}

/**
 * 富文本编辑器的隐藏和显示及内容赋予
 * @param env
 * @param key // 是否是第一次点击 布尔值 false或true
 */
function bindClick(env, key) {
    env.onclick = function(){

        var edit        = document.getElementsByClassName('note-editor')[0];    // 编辑器对象
        var editTexe    = document.getElementsByClassName('note-editable')[0];  // 编辑器文本区域
        var index       = checkString(edit.getAttribute('class'), 'piao');

        if(index) {
            edit.setAttribute("class", 'note-editor');
        } else {
            var place = [
                ['tag', 'body'],
                ['class', 'note-editor'],
                ['class', 'note-editor']
            ];
            var tag = [
                ['div'],
                ['button'],
                ['button']
            ];
            var attr = [
                ['class'],
                ['type', 'class'],
                ['type', 'class', 'id']
            ];
            var values = [
                ['note-editor-background'],
                ['submit', 'hidd btn btn-default'],
                ['button', 'hidd btn btn-default', 'reset']
            ];
            var text = [
                [''],
                ['确认'],
                ['放弃']
            ];
            createNote(place, tag, attr, values, text, 3);

            var reset = document.getElementById('reset');

            reset.onclick = function () {
                hid(1);
            };

            hid();
            var info             = env.value;
            var childeNode       = editTexe.firstChild;
            childeNode.innerHTML = info;
            edit.setAttribute("class", 'note-editor piao');
        }
    };
    if(key) {
        env.onclick();
    }
}

/**
 * 创建子节点
 * @param place // 父节点 数组第一个值为类型 第二个值为名
 * @param tag // 标签
 * @param attr // 属性名 数组值
 * @param value // 属性值 数组值
 * @param text // 节点内容
 */
function createNote(place, tag, attr, value,  text, sums) {


    for(var a = 0; a < sums; a++) {
        var liElement   = document.createElement(tag[a][0]);
        var index = attr[a].length;
        for(var i = 0; i < index; i++) {
            liElement.setAttribute(attr[a][i], value[a][i]);
        }
        var liText = document.createTextNode(text[a][0]);

        liElement.appendChild(liText);

        var parent;
        switch (place[a][0]) {
            case 'id':
                parent = document.getElementById(place[a][1]);
                break;
            case 'class':
                parent = document.getElementsByClassName(place[a][1])[0];
                break;
            case 'tag':
                parent = document.getElementsByTagName(place[a][1])[0];
                break;
            case 'Attr':
                parent = document.getAnonymousElementByAttribute(place[a][1])[0];
                break;
        }

        parent.appendChild(liElement);

    }
}

/**
 * 点击非编辑器区域隐藏编辑器
 * @param env
 */
function hid(env) {

    var edit = document.getElementsByClassName('note-editor')[0];    // 编辑器对象
    var info = document.getElementsByClassName('note-editor-background')[0];
    var hidd = document.getElementsByClassName('hidd');

    info.onclick = function () {
        info.setAttribute("class", '');
        edit.setAttribute("class", 'note-editor');
        var index = hidd.length;
        for (var i = 0; i < index; i++ ) {
            edit.removeChild(hidd[0]);
        }
    }
    if(env) {
        info.onclick();
    }

}

